package com.sinodata.bsm.cicp.finder;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import com.sinodata.bsm.cicp.util.ProtocolParameterUtil;
import com.sinodata.bsm.common.constants.SnmpConstants;
import com.sinodata.bsm.common.vo.ProtocolParameter;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-9-3 下午3:44:41          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@SuppressWarnings("rawtypes")
public abstract class SnmpFinder extends Finder {

    protected String ip = null;
    protected Map params = null;

    @Override
    public List<String[]> find(String ip, Map params) {
        setIp(ip);
        setParamMap(params);
        ProtocolParameter parameter = ProtocolParameterUtil.getSnmpValue(ip);

        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(parameter.getUserName()));
        Address localAddress = GenericAddress.parse("udp:" + ip.trim() + "/" + parameter.getPort());
        target.setAddress(localAddress);
        target.setRetries(0);
        target.setTimeout(5000);
        target.setVersion(SnmpConstants.getVersion(parameter.getProtocol()));

        Snmp snmp = null;
        try {
            snmp = new Snmp(new DefaultUdpTransportMapping());
            snmp.listen();
            return find(snmp, target);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (snmp != null) {
                try {
                    snmp.close();
                } catch (Exception e2) {
                }
            }
        }
    }

    public abstract List<String[]> find(Snmp snmp, CommunityTarget target);

    private void setIp(String ip) {
        this.ip = ip;
    }

    private void setParamMap(Map params) {
        this.params = params;
    }

    public static void main(String[] args) {

    }

}
